package com.xuwang.service.impl;

import com.xuwang.Util.DateTimeUtils;
import com.xuwang.Util.JWTutil;
import com.xuwang.Util.RandomNumberUtil;
import com.xuwang.mapper.AdditiveMapper;
import com.xuwang.mapper.InventoryMapper;
import com.xuwang.mapper.ProductionMapper;
import com.xuwang.pojo.FoodRelation;
import com.xuwang.pojo.Inventory;
import com.xuwang.pojo.Waste;
import com.xuwang.mapper.WasteMapper;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 废弃物表(Waste)表服务实现类
 *
 * @author abaaba
 * @since 2021-09-20 15:33:35
 * @version 1.0
 */
@Service("wasteService")
public class WasteServiceImpl {
    @Resource
    private WasteMapper wasteMapper;
    @Resource
    private AdditiveMapper additiveMapper;
    @Resource
    private ProductionMapper productionMapper;
    @Resource
    private InventoryMapper inventoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        String batchNo = wasteMapper.selectBatchNoById(id);
        Integer type = productionMapper.selectMatchTypeByBatchNo(batchNo);

        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.wasteMapper.selectById(id));
        map.put("food", this.productionMapper.selectByBatchNo(batchNo,type));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int m , int index, int page, Integer type,
                                             String batchNo,String name,String companyName, HttpServletRequest request) {
    // 获取当前表中的总记录
        if(m==2){
            String token = request.getHeader("Authorization");
            String userName = JWTutil.getUserName(token);

//            String userName="菜品人";
            companyName=additiveMapper.selectCompanyNameByUsername(userName);
        }
        // 获取当前表中的总记录
        int tableCount = this.wasteMapper.selectForCount(type,batchNo,name,companyName);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / page + 1;
        // 计算每页开始的下标值
        index = (index - 1) * page;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.wasteMapper.selectForPage(index, page,type,batchNo,name,companyName));
        return map;
    }

    /**
     * 新增数据
     *
     * @param waste 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(Waste waste,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userName = JWTutil.getUserName(token);
//        String userName="菜品人";
        Map<String, Object> map = new HashMap<>();
        waste.setWasteCompanyName(additiveMapper.selectCompanyNameByUsername(userName));
        int companyId = additiveMapper.selectCompanyIdByUsername(userName);

        Inventory inventory = inventoryMapper.selectIdAndNumberByNoAndCompanyId(waste.getWasteBatchNo(), companyId);//插入数据到库存中
        System.out.println(inventory.getNumber());
        map.put("code", 201);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "废弃失败，库存不够");
        if (inventory!=null){
        if (inventory.getNumber()>=waste.getNumber()) {
            inventoryMapper.updateNumberByNoAndCompanyId(inventory.getNumber()-waste.getNumber(), waste.getWasteBatchNo(), companyId);
            waste.setWasteTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
             wasteMapper.insert(waste);
            map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", "废弃成功");
        }}
        return map;
    }




}